<?php
/**
 * 附件管理
 *
 */
class att_manage
{
	private $table;
	private $db;
	public $pages;
	
	function att_manage()
	{
		global $db;
		$this->table = DB_PRE.'attachment';
		$this->db=&$db;
	}
	
	/**
	 * 获取附件列表
	 *
	 * @param int $page
	 * @param int $pagesize
	 * @param varchar $name
	 * @param varchar $ext
	 * @param varchar $start
	 * @param varchar $end
	 * @param int $sizes
	 * @param int $sizee
	 * @return array
	 */
	function get_atts($page,$pagesize,$name,$ext,$start,$end,$sizes,$sizee)
	{
		$start_record = ($page - 1) * $pagesize;
		$search_array = array();
		if(!empty($name))
		{
			$search_array['filename like ?'] = array('set_string', '%'.$name.'%');
		}
		if(!empty($ext))
		{
			$search_array['fileext=?'] = array('set_string', $ext);
		}
		if(!empty($start))
		{
			$search_array['uploadtime>=?'] = array('set_int', strtotime($start . ' 00:00:00'));
		}
		if(!empty($end))
		{
			$search_array['uploadtime<=?'] = array('set_int', strtotime($end . ' 23:59:59'));
		}
		if(intval($sizes) > 0)
		{
			$search_array['filesize>=?'] = array('set_int', $sizes*1024);
		}
		if(intval($sizee) > 0)
		{
			$search_array['filesize<=?'] = array('set_int', $sizee*1024);
		}
		if(sizeof($search_array) > 0)
		{
			$where = ' where ' . implode(' and ', array_keys($search_array));
		}
		else 
		{
			$where = ' where 1=1';
		}
        $where .= " and status=".STATUS_NORMAL;
		$this->db->prepare('select count(*) as nums from '.$this->table . $where);
		if(sizeof($search_array) > 0)
		{
			foreach ($search_array as $v)
			{
				$func = $v[0];
				$this->db->$func($v[1]);
			}
		}
		$c_res = $this->db->execute(true);
		$this->db->prepare('select * from '.$this->table . $where . ' order by uploadtime desc limit ?,?');
		if(sizeof($search_array) > 0)
		{
			foreach ($search_array as $v)
			{
				$func = $v[0];
				$this->db->$func($v[1]);
			}
		}
		$this->db->set_int($start_record);
		$this->db->set_int($pagesize);
		$res = $this->db->execute();
		$cout = $c_res['nums'];
		$this->pages = pages($cout,$page,$pagesize);
		return $res;
	}
	
	/**
	 * 获得现有的附件类型
	 *
	 * @return array
	 */
	function get_file_types()
	{
		$res = $this->db->query('select fileext from ' . $this->table . ' where status='.STATUS_NORMAL.' group by fileext');
		$list = array();
		if(is_array($res))
		{
			foreach ($res as $v)
			{
				$list[$v['fileext']] = $v['fileext'];
			}
		}
		return $list;
	}
	
	/**
	 * 删除一个附件
	 *
	 * @param  int $id
	 * @return bool
	 */
	function delete_att($id)
	{
		$this->db->prepare('select filepath,siteid from ' . $this->table . ' where `id`=? and status=?');
		$this->db->set_string($id);
		$this->db->set_string(STATUS_NORMAL);
		$info = $this->db->execute(true);
		if(!isset($info['filepath']))
		{
			return true;
		}
        $filepath = site_root($info['siteid'],1).$info['filepath'];
		if(file_exists($filepath))
		{
			unlink($filepath);
		}
		return $this->_delete_att($id);
	}
	
	/**
	 * 删除一条附件记录
	 *
	 * @param int $id
	 * @return bool
	 */
	function _delete_att($id)
	{
        return $this->db->fake_delete($this->table, array('id'=>$id));
	}
}
?>